<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
      <el-form-item label="设备名称" prop="deviceName" >
        <el-input placeholder="请输入设备名称" v-model="state.queryForm.deviceName" />
      </el-form-item>
      <el-form-item label="运行状态" prop="runningStatus" >
        <el-input placeholder="请输入运行状态" v-model="state.queryForm.runningStatus" />
      </el-form-item>
      <el-form-item label="平层状态" prop="levelingStatus">
            <el-select v-model="state.queryForm.levelingStatus" placeholder="请选择平层状态">
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in evabox_leveling_status" :key="index"></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="门状态" prop="doorStatus">
            <el-select v-model="state.queryForm.doorStatus" placeholder="请选择门状态">
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in evabox_door_status" :key="index"></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="运行速度" prop="speed" >
        <el-input placeholder="请输入运行速度" v-model="state.queryForm.speed" />
      </el-form-item>
      <el-form-item label="气压" prop="press" >
        <el-input placeholder="请输入气压" v-model="state.queryForm.press" />
      </el-form-item>
      <el-form-item label="电瓶车状态" prop="motoStatus">
            <el-select v-model="state.queryForm.motoStatus" placeholder="请选择电瓶车状态">
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in evabox_moto_status" :key="index"></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="人数" prop="personCount" >
        <el-input placeholder="请输入人数" v-model="state.queryForm.personCount" />
      </el-form-item>
      <el-form-item label="温度" prop="tempe" >
        <el-input placeholder="请输入温度" v-model="state.queryForm.tempe" />
      </el-form-item>
      <el-form-item label="湿度" prop="humid" >
        <el-input placeholder="请输入湿度" v-model="state.queryForm.humid" />
      </el-form-item>
      <el-form-item label="二氧化碳浓度" prop="co2" >
        <el-input placeholder="请输入二氧化碳浓度" v-model="state.queryForm.co2" />
      </el-form-item>
      <el-form-item label="烟雾状态" prop="smokeStatus">
            <el-select v-model="state.queryForm.smokeStatus" placeholder="请选择烟雾状态">
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in evabox_smoke_status" :key="index"></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="火焰状态" prop="flameStatus">
            <el-select v-model="state.queryForm.flameStatus" placeholder="请选择火焰状态">
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in evabox_flame_status" :key="index"></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="总累计运行次数" prop="totalRunningCount" >
        <el-input placeholder="请输入总累计运行次数" v-model="state.queryForm.totalRunningCount" />
      </el-form-item>
      <el-form-item label="总累计开门次数" prop="totalRunningOpenCount" >
        <el-input placeholder="请输入总累计开门次数" v-model="state.queryForm.totalRunningOpenCount" />
      </el-form-item>
      <el-form-item label="总累计里程" prop="totalRunningMile" >
        <el-input placeholder="请输入总累计里程" v-model="state.queryForm.totalRunningMile" />
      </el-form-item>
      <el-form-item label="总累计运行时间" prop="totalRunningTime" >
        <el-input placeholder="请输入总累计运行时间" v-model="state.queryForm.totalRunningTime" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" >
            <el-date-picker type="datetime" placeholder="请输入创建时间" v-model="state.queryForm.createTime"></el-date-picker>
      </el-form-item>
      <el-col/>
      <el-form-item label="修改时间" prop="updateTime" >
            <el-date-picker type="datetime" placeholder="请输入修改时间" v-model="state.queryForm.updateTime"></el-date-picker>
      </el-form-item>
      <el-col/>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'evaboxcloud_deviceMetaData_add'">
            新 增
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
            v-auth="'evaboxcloud_deviceMetaData_del'" @click="handleDelete(selectObjs)">
            删除
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'evaboxcloud_deviceMetaData_export'"
                @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border 
        :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" label="#" width="40" />
          <el-table-column prop="deviceName" label="设备名称"  show-overflow-tooltip/>
          <el-table-column prop="errorCode" label="异常码"  show-overflow-tooltip/>
          <el-table-column prop="floorNumber" label="楼层编号"  show-overflow-tooltip/>
          <el-table-column prop="floorName" label="楼层名称"  show-overflow-tooltip/>
          <el-table-column prop="runningStatus" label="运行状态" show-overflow-tooltip>
      <template #default="scope">
                <dict-tag :options="evabox_running_status" :value="scope.row.runningStatus"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="levelingStatus" label="平层状态" show-overflow-tooltip>
      <template #default="scope">
                <dict-tag :options="evabox_leveling_status" :value="scope.row.levelingStatus"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="doorStatus" label="门状态" show-overflow-tooltip>
      <template #default="scope">
                <dict-tag :options="evabox_door_status" :value="scope.row.doorStatus"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="speed" label="运行速度" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="press" label="气压" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="motoStatus" label="电瓶车状态" show-overflow-tooltip>
      <template #default="scope">
                <dict-tag :options="evabox_moto_status" :value="scope.row.motoStatus"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="personCount" label="人数" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="tempe" label="温度" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="humid" label="湿度" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="co2" label="二氧化碳浓度" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="smokeStatus" label="烟雾状态" show-overflow-tooltip>
      <template #default="scope">
                <dict-tag :options="evabox_smoke_status" :value="scope.row.smokeStatus"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="flameStatus" label="火焰状态" show-overflow-tooltip>
      <template #default="scope">
                <dict-tag :options="evabox_flame_status" :value="scope.row.flameStatus"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="totalRunningCount" label="总累计运行次数" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="totalRunningOpenCount" label="总累计开门次数" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="totalRunningMile" label="总累计里程" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="totalRunningTime" label="总累计运行时间" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="createTime" label="创建时间" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="updateTime" label="修改时间" sortable="custom" show-overflow-tooltip/>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button icon="edit-pen" text type="primary" v-auth="'evaboxcloud_deviceMetaData_edit'"
              @click="formDialogRef.openDialog(scope.row.id)">编辑</el-button>
            <el-button icon="delete" text type="primary" v-auth="'evaboxcloud_deviceMetaData_del'" @click="handleDelete([scope.row.id])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

  </div>
</template>

<script setup lang="ts" name="systemDeviceMetaData">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs } from "/@/api/evaboxcloud/deviceMetaData";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

const { evabox_door_status,evabox_moto_status,evabox_flame_status,evabox_leveling_status,evabox_running_status,evabox_smoke_status } = useDict('evabox_door_status','evabox_moto_status','evabox_flame_status','evabox_leveling_status','evabox_running_status','evabox_smoke_status')
// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/evaboxcloud/deviceMetaData/export',Object.assign(state.queryForm, { ids: selectObjs }), 'deviceMetaData.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>